package dk.aau.walkietalkieui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class Base_FragmentActivity extends FragmentActivity {
	private static final int RESULT_SETTINGS = 1;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.action_settings:
			Intent i = new Intent(this, SettingsActivity.class);
			startActivityForResult(i, RESULT_SETTINGS);
			break;

		case R.id.action_refresh:
			Toast.makeText(getApplicationContext(), "Refresh",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.action_help:
			Toast.makeText(getApplicationContext(), "Help me!",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.action_about:
			Toast.makeText(getApplicationContext(), "Designed by us",
					Toast.LENGTH_SHORT).show();
			break;
		}

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		
		SharedPreferences sharedPref = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());
		Boolean syncConnPref = sharedPref.getBoolean(
				SettingsActivity.KEY_PREF_SYNC_CONN, false);

		MenuItem actionStatus = menu.findItem(R.id.action_status);

		if (syncConnPref) {
			actionStatus.setTitle("Online");
		//	actionStatus.setIcon(getApplicationContext().getResources()
			//		.getDrawable(R.drawable.online));

		} else {

			actionStatus.setTitle("Offline");
			//actionStatus.setIcon(getApplicationContext().getResources()
				//	.getDrawable(R.drawable.broadcast));
		}
	

		return super.onPrepareOptionsMenu(menu);
	}

	@SuppressLint("NewApi")
	@Override
	protected void onResume() {
		super.onResume();
		int currentapiVersion = android.os.Build.VERSION.SDK_INT;
		//deal with the compatibility
		if (currentapiVersion > android.os.Build.VERSION_CODES.HONEYCOMB){
			invalidateOptionsMenu();
		} 
		
	}

}
